﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mimoza.GUI;

namespace Mimoza.VNCPlugin
{
    public partial class VNCSettingsPage : UserControl, Mimoza.GUI.IWizardPage
    {
        VNCPluginData m_data;

        protected void RaiseOnPageModified()
        {
            PageEvent handler = OnPageModified;

            if (handler != null)
            {
                handler(this);
            }
        }

        public VNCSettingsPage()
        {
            InitializeComponent();
        }

        #region IWizardPage Members
        private IWizard m_wizard;

        private bool m_IsConfigured = true;

        public void Init(Mimoza.GUI.IWizard wizard)
        {
            m_wizard = wizard;
            m_data = this.Wizard.UserData as VNCPluginData;

            EnterPage();

            this.Confirmation.TextChanged += new System.EventHandler(this.Confirmation_TextChanged);
            this.Password.TextChanged += new System.EventHandler(this.Password_TextChanged);
            this.checkBoxEnableVNC.CheckStateChanged += new System.EventHandler(this.checkBoxEnableVNC_CheckedChanged);
        }

        public void EnterPage()
        {
            checkBoxEnableVNC.Checked = m_data.IsVncEnabled;
            Password.Text = m_data.VNCPassword;
            Confirmation.Text = m_data.VNCPassword;
            checkBoxEnableVNC_CheckedChanged(null, null);
        }

        public bool Try2LeavePage(WizardPageLeavingDirection direction)
        {
            if (direction == WizardPageLeavingDirection.Back)
            {
                return true;
            }

            m_data.IsVncEnabled = checkBoxEnableVNC.Checked;

            if (m_data.IsVncEnabled)
            { 
                if ( this.Password.Text != null && this.Password.Text.Length > 0)
                {
                    if (this.Confirmation.Text == null || this.Confirmation.Text.Length == 0)
                    {
                        return false;
                    }
                    if (this.Password.Text != this.Confirmation.Text)
                    {
                        return false;
                    }
                }
            }

            m_data.VNCPassword = Password.Text;

            return true;
        }

        public Mimoza.GUI.IWizard Wizard
        {
            get
            {
                return m_wizard;
            }
        }

        public string PageName
        {
            get { return Resource.VNCPageName; }
        }

        public string PageTitle
        {
            get { return Resource.VNCPageNameTitle; }
        }

        public bool IsConfigured
        {
            get
            {
                return m_IsConfigured;
            }
            set
            {
                m_IsConfigured = value;
            }
        }

        public event Mimoza.GUI.PageEvent OnPageModified;

        #endregion

        private void checkBoxEnableVNC_CheckedChanged(object sender, EventArgs e)
        {
            this.Password.Enabled = checkBoxEnableVNC.Checked;
            this.Confirmation.Enabled = checkBoxEnableVNC.Checked;
            
            if (null != sender)
            {
                this.RaiseOnPageModified();
            }
        }

        private void Password_TextChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }

        private void Confirmation_TextChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }
    }
}
